package com.example.bgmachine.bgmachine.clearpopups;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.homes.HomePages;

import com.example.bgmachine.bgmachine.sets.ClearMenuItemData;

import java.util.LinkedList;

public class ClearpupAdapter extends BaseAdapter {

    private Context mContext;
    private LinkedList<ClearpupData> mData = new LinkedList<ClearpupData>();

    public ClearpupAdapter(Context context, LinkedList<ClearpupData> list) {
        this.mContext = context;
        this.mData = list;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // txt_pin txt_content
    @Override
    public View getView(int position, View convertViewtms, ViewGroup parent) {
        ClearpupAdapter.ViewHolder holder = null;
        if (convertViewtms == null) {
            View convertView = View.inflate(mContext, R.layout.clear_view_select_list_item_content, null);
            convertViewtms = convertView;

            holder = new ClearpupAdapter.ViewHolder();
            holder.mClearpupData = mData.get(position);
            holder.clear_view_popup_name_textview = convertViewtms.findViewById(R.id.clear_view_popup_name_textview);
            holder.clear_view_popup_name_textview.setText( holder.mClearpupData.getClearName()  );
            holder.clear_view_popup_current_progressbar = convertViewtms.findViewById(R.id.clear_view_popup_current_progressbar);
            holder.clear_view_popup_current_progressbar.setMax( holder.mClearpupData.getMaxProgress());
            holder.clear_view_popup_current_progressbar.setProgress(holder.mClearpupData.getCurrentProgress());
//            holder.frament_clear_list_content_bottom_btn_operation_click = convertViewtms.findViewById(R.id.frament_clear_list_content_bottom_btn_operation_click);
//            holder.frament_clear_list_content_bottom_btn_operation_click.setTag(holder);
//
//            holder.frament_clear_list_content_bottom_btn_operation_click.setOnClickListener(new View.OnClickListener() {
//                public void onClick(View v) {
//
//                    ClearpupAdapter.ViewHolder vs = (ClearpupAdapter.ViewHolder) v.getTag();
//
//                    if (vs == null) {
//                        Log.d("aaa", " holder.mOrderContentData.getOrderNumber()==cc=");
//                        return;
//                    }
//                    HomePages.mHomePages.setClearpupView("");
//                }
//            });
//            holder.frament_clear_list_content_bottom_btn_operation_click.setText(holder.mClearpupData.getClearName());
            convertView.setTag(holder);
        } else {

            holder = (ClearpupAdapter.ViewHolder) convertViewtms.getTag();
            holder.mClearpupData = mData.get(position);
            holder.clear_view_popup_name_textview = convertViewtms.findViewById(R.id.clear_view_popup_name_textview);
            holder.clear_view_popup_name_textview.setText( holder.mClearpupData.getClearName()  );
            holder.clear_view_popup_current_progressbar = convertViewtms.findViewById(R.id.clear_view_popup_current_progressbar);
            holder.clear_view_popup_current_progressbar.setMax( holder.mClearpupData.getMaxProgress());
            holder.clear_view_popup_current_progressbar.setProgress(holder.mClearpupData.getCurrentProgress());
//            holder.frament_clear_list_content_bottom_btn_operation_click = convertViewtms.findViewById(R.id.frament_clear_list_content_bottom_btn_operation_click);
//            holder.frament_clear_list_content_bottom_btn_operation_click.setTag(holder);
//            holder.frament_clear_list_content_bottom_btn_operation_click.setText(holder.mClearpupData.getClearName());


        }

        return convertViewtms;
    }

    //添加一个元素
    public void add(ClearpupData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, ClearpupData data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void remove(ClearpupData data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }


    private class ViewHolder {

        public ProgressBar clear_view_popup_current_progressbar = null;
        public TextView clear_view_popup_name_textview = null;

        public ClearpupData mClearpupData = null;

    }

}
